//
//  BHSingleTextField.m
//  Behing
//
//  Created by Lj on 2017/10/26.
//  Copyright © 2017年 lj. All rights reserved.
//

#import "BHSingleTextField.h"

@interface BHSingleTextField ()

@property (nonatomic, copy) ActionText actionBlock;
@property (nonatomic, copy) NSString * image1;
@property (nonatomic ,copy) NSString * image2;
@property BOOL a;

@end

@implementation BHSingleTextField

- (id)init {
    if (self = [super init]) {
    }
    return self;
}

- (BHSingleTextField *(^)(NSString *placeholder))placeholderStr {
    return ^id(NSString *placeholder) {
        self.placeholder = placeholder;
        return self;
    };
}

- (BHSingleTextField *(^)(NSString *text))textStr {
    return ^id(NSString *text) {
        self.text = text;
        return self;
    };
}

- (BHSingleTextField *(^)(UIColor *backgroundColor))backColor {
    return ^id(UIColor *backgroundColor) {
        self.backgroundColor = backgroundColor;
        return self;
    };
}

- (BHSingleTextField *(^)(NSString *imageStr, CGFloat height))leftImage {
    return ^id(NSString *imageStr, CGFloat height) {
        [self _setTextFieldattributeImageStr:imageStr height:height];
        return self;
    };
}

- (BHSingleTextField *(^)(UIFont *font))text_font {
    return ^id(UIFont *font) {
        self.font = font;
        return self;
    };
}

- (BHSingleTextField *(^)(CGFloat margin))leftMargin {
    return ^id(CGFloat margin) {
        [self _setTextFieldLeftMargin:margin];
        return self;
    };
}

- (BHSingleTextField *(^)(BOOL isMM))isMm {
    return ^id(BOOL isMm) {
        self.secureTextEntry = isMm;
        return self;
    };
}
- (BHSingleTextField *(^)(UIColor *borderColor, CGFloat borderWidth, CGFloat cornerRadius))border {
    return ^id(UIColor *borderColor, CGFloat borderWidth, CGFloat cornerRadius) {
        self.layer.cornerRadius = cornerRadius;
        self.layer.borderColor = borderColor.CGColor;
        self.layer.borderWidth = borderWidth;
        return self;
    };
}

- (BHSingleTextField *(^)(ActionText action))action {
    return ^id(ActionText action) {
        [self addTarget:self action:@selector(textChangeClick:) forControlEvents:UIControlEventEditingChanged];
        self.actionBlock = action;
        return self;
    };
}

- (BHSingleTextField *(^)(NSString *imageStr,NSString *imageStr2, CGFloat height,BOOL isShow))rightBtn {
    return ^id(NSString *imageStr,NSString *imageStr2, CGFloat height,BOOL isShow){
        [self _setTextFiledBtn:imageStr btnImg:imageStr2 height:height isShow:isShow];
        return self;
    };
}

//
-(void)_setTextFiledBtn:(NSString *)imageStr btnImg:(NSString *)btnImg height:(CGFloat)height isShow:(BOOL)isShow{
    
    UIView *rightView = [[UIView alloc]initWithFrame:CGRectMake(-40, -40, 40, height)];
    self.image1 = imageStr;
    self.image2 = btnImg;
    
    UIButton *rightBtn = [[UIButton alloc]init];
    self.a = YES;
    [rightBtn setImage:[UIImage imageNamed:_image1] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(secureSwitchAction:) forControlEvents:UIControlEventTouchUpInside];
    [rightView addSubview:rightBtn];
    [rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(rightView);
        make.height.width.offset(20);
        make.right.equalTo(rightView).offset(-5);
    }];
    self.contentMode = UIViewContentModeCenter;
    self.rightViewMode = UITextFieldViewModeAlways;
    self.rightView = rightView;
    self.rightView.hidden = YES;
    if (isShow == NO) {
        self.rightView.hidden = NO;
    }
    
}
- (void)secureSwitchAction:(id)sender {
    UIButton * rightBtn = (UIButton *)sender;
    if (self.a == YES)
    {
        [rightBtn setImage:[UIImage imageNamed:_image2] forState:UIControlStateNormal];
        self.a = NO;
    }else{
        self.a = YES;
        [rightBtn setImage:[UIImage imageNamed:_image1] forState:UIControlStateNormal];
    }
    self.secureTextEntry = !self.secureTextEntry;
    NSString* text = self.text;
    self.text = @" ";
    self.text = text;
}

- (void)_setTextFieldLeftMargin:(CGFloat)leftMargin {
    UIView *leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, leftMargin, 0)];
    self.contentMode = UIViewContentModeCenter;
    self.leftViewMode = UITextFieldViewModeAlways;
    self.leftView = leftView;
}

//设置TestField属性
-(void)_setTextFieldattributeImageStr:(NSString *)str height:(CGFloat)height {
    UIView *leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 40, height)];
    UIImageView *leftImage = [[UIImageView alloc]init];
    leftImage.image = [UIImage imageNamed:str];
    [leftView addSubview:leftImage];
    
    [leftImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(leftView);
        make.width.offset(20);
        make.height.offset(28);
        make.left.equalTo(leftView).offset(12);
    }];
    
    if (![BHSettingUtil dataAndStringIsNull:self.leftTitle]) {
        UILabel *label = [[UILabel alloc] init];
        label.text = self.leftTitle;
        label.font = BHFont_Size_15;
        [leftView addSubview:label];
        
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(leftView);
            make.left.equalTo(leftImage.mas_right).offset(10);
        }];
        
        CGFloat width = [BHSettingUtil widthForText:self.leftTitle fontSize:15];
        CGRect frame = CGRectMake(0, 0, 40+width, height);
        leftView.frame = frame;
    }
    
    self.contentMode = UIViewContentModeCenter;
    self.leftViewMode = UITextFieldViewModeAlways;
    self.leftView = leftView;
}

- (void)textChangeClick:(UITextField *)textField {
    if (self.actionBlock) {
        self.actionBlock(textField.text);
    }
}

@end
